<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="ThemeBucket">
    <link rel="shortcut icon" href="#" type="image/png">

    <title>Form Layouts</title>

    <link href="<%=path%>/resources/css/style.css" rel="stylesheet">
    <link href="<%=path%>/resources/css/style-responsive.css" rel="stylesheet">
    <link href="<%=path%>/resources/js/bootstrap-datetimepicker/css/datetimepicker.css">
    <link href="<%=path%>/resources/css/common.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="<%=path%>/resources/js/html5shiv.js"></script>
    <script src="<%=path%>/resources/js/respond.min.js"></script>
    <![endif]-->
</head>

<body class="sticky-header">

<section>
    <!-- left side start-->
    <%@ include file="/page/common/leftmenu.jsp" %>
    <!-- left side end-->

    <!-- main content start-->
    <div class="main-content" >

        <!-- header section start-->
        <%@ include file="/page/common/top.jsp" %>
        <!-- header section end-->

        <!-- page heading start-->
        <div class="page-heading">
            <h3>

            </h3>
            <ul class="breadcrumb">
                <li>
                    <a href="#">管理平台</a>
                </li>
                <li class="active">订单管理</li>
            </ul>
        </div>
        <!-- page heading end-->

        <!--body wrapper start-->
        <section class="wrapper">
            <!-- page start-->
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel">
                        <div class="panel-body toolbar">
                            <style>
                                .label-right label{padding-right: 0;margin-top: 5px;width: 90px}
                            </style>
                            <div class="row label-right">
                                <label class="col-md-1 control-label font-inherit"> 订单状态：</label>
                                <div class="col-md-2" style="padding-left: 0">
                                    <select id="state-select" class="form-control selectpicker" data-live-search="true">
                                        <option value="-1">全部</option>
                                    </select>
                                </div>
                                <label class="col-md-1 control-label font-inherit"> 业务类型A：</label>
                                <div class="col-md-2" style="padding-left: 0">
                                    <select id="bus-one-select" class="form-control selectpicker" data-live-search="true">
                                        <option value="-1">全部</option>
                                    </select>
                                </div>
                                <label class="col-md-1 control-label font-inherit"> 业务类型B：</label>
                                <div class="col-md-2" style="padding-left: 0">
                                    <select id="bus-two-select" class="form-control selectpicker" data-live-search="true">
                                        <option value="-1">全部</option>
                                    </select>
                                </div>
                                <label class="col-md-1 control-label font-inherit"> 业务类型C：</label>
                                <div class="col-md-2" style="padding-left: 0">
                                    <select id="bus-three-select" class="form-control selectpicker" data-live-search="true">
                                        <option value="-1">全部</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row label-right" style="margin-top: 20px">
                                <label class="col-md-1 control-label font-inherit"> 省：</label>
                                <div class="col-md-2" style="padding-left: 0">
                                    <select id="province-select" class="form-control selectpicker" data-live-search="true">
                                        <option value="-1">全部</option>
                                    </select>
                                </div>
                                <label class="col-md-1 control-label font-inherit"> 市：</label>
                                <div class="col-md-2" style="padding-left: 0">
                                    <select id="city-select" class="form-control selectpicker" data-live-search="true">
                                        <option value="-1">全部</option>
                                    </select>
                                </div>
                                <label class="col-md-1 control-label font-inherit"> 区：</label>
                                <div class="col-md-2" style="padding-left: 0">
                                    <select id="area-select" class="form-control selectpicker" data-live-search="true">
                                        <option value="-1">全部</option>
                                    </select>
                                </div>
                                <label class="col-md-1 control-label font-inherit"> 订单编号：</label>
                                <div class="col-md-2" style="padding-left: 10px">
                                    <input id="orderNo" class="form-control" type="text" placeholder="请输入订单号">
                                </div>
                            </div>
                            <div class="row label-right" style="margin-top: 20px">
                                <label class="col-md-1 control-label font-inherit"> 手机号：</label>
                                <div class="col-md-2" style="padding-left: 10px">
                                    <input id="phone" class="form-control" type="text" placeholder="请输入手机号">
                                </div>
                                <label class="col-md-1 control-label font-inherit"> 订单时间：</label>
                                <div class="col-md-2 input-append date" style="padding-left: 10px" data-date-format="yyyy-MM-dd">
                                    <input id="startTime" class="form-control" type="text">
                                </div>
                                <label class="col-md-1 control-label font-inherit">至</label>
                                <div class="col-md-2" style="padding-left: 10px">
                                    <input id="endTime" class="form-control" type="text">
                                </div>
                                <div class="col-md-2">
                                    <span class="input-group-btn">
                                        <button type="button"  onclick="search()" class="btn btn-primary">查询</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <%--</div>--%>
            <div class="table table-striped table-hover table-bordered" id="editable-sample">
                <div class="table-responsive" data-pattern="priority-columns" data-focus-btn-icon="fa-asterisk"
                     data-sticky-table-header="true" data-add-display-all-btn="true" data-add-focus-btn="true">
                    <table id="table" data-unique-id="id"
                           data-pagination="true"
                           data-id-field="id"
                           data-maintain-selected="true"
                           data-page-size="10"
                           data-page-list="[5,10,15,20]"
                           data-side-pagination="server">
                    </table>
                </div>
            </div>

                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                                    &times;
                                </button>
                                <h4 class="modal-title" id="myModalLabel">
                                    待派单师傅
                                </h4>
                            </div>
                            <div class="modal-body">
                                <div class="form-group ">
                                    <input type="hidden" id="orderNo2" name="orderNo2" />
                                    <label class="control-label col-lg-2">师傅</label>
                                    <div class="col-lg-10">
                                        <select id="worker-select" name="worker" class="form-control selectpicker" data-live-search="true">
                                            <option value="-1">-请选择-</option>
                                            <%--<c:forEach var="w" items="${workers}">
                                                <option value="${w.id}">${w.wName}</option>
                                            </c:forEach>--%>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                <button type="button" id="dispatch" class="btn btn-primary">提交</button>
                            </div>
                        </div><!-- /.modal-content -->
                    </div><!-- /.modal -->
                </div>
            </div>
            <!-- page end-->
        </section>


        <!--body wrapper end-->

        <!--footer section start-->
        <%@ include file="/page/common/footer.jsp" %>
        <!--footer section end-->


    </div>
    <!-- main content end-->
</section>

<!-- Placed js at the end of the document so the pages load faster -->
<script src="<%=path%>/resources/js/jquery-1.10.2.min.js"></script>
<script src="<%=path%>/resources/js/jquery-ui-1.9.2.custom.min.js"></script>
<script src="<%=path%>/resources/js/jquery-migrate-1.2.1.min.js"></script>
<script src="<%=path%>/resources/js/bootstrap.min.js"></script>
<script src="<%=path%>/resources/js/bootstrap.min.js"></script>
<script src="<%=path%>/resources/js/bootstrap-table.js"></script>

<script src="<%=path%>/resources/js/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>



<script src="<%=path%>/resources/js/modernizr.min.js"></script>
<script src="<%=path%>/resources/js/jquery.nicescroll.js"></script>
<script src="<%=path%>/resources/js/common.js"></script>

<!--common scripts for all pages-->
<script src="<%=path%>/resources/js/scripts.js"></script>
<script>
    var offset = -1;
    var $table = $('#table'), selections = [];
    $(function () {
        var date = "${date}";
        var stateV = ${state};
        if (date != "") {
            $("#startTime").val(date);
            $("#endTime").val(date);
            /*$("#startTime").value = date;
            $("#endTime").value = date;*/
        }
        init_province();
        init_one_bus_type();
        init_state(stateV);
        initData();
    });

    //初始化状态
    function init_state(stateV) {
        $.ajax({
            url: '<%=path %>/admin/order/query_state.do',
            async: false,
            success: function (data) {
                $.each(data.data, function (idx, obj) {
                    var value = obj.v;
                    if (value == stateV) {
                        $('#state-select').append('<option value=' + value + ' selected>' + obj.o + '</option>')
                    } else {
                        $('#state-select').append('<option value=' + value + '>' + obj.o + '</option>')
                    }
                });
            }
        });
    }
    
    $(function () {
        //$('#startTime').datepicker({format:'yyyy-mm-dd'});
        $('#startTime').datepicker({dateFormat: 'yy-mm-dd'})
        $('#endTime').datepicker({dateFormat: 'yy-mm-dd'})
        /*$('#endTime').datetimepicker({
            dateFormat: 'yy-mm-dd',
            showSecond: true, //显示秒
            timeFormat: 'HH:mm:ss',//格式化时间
        })*/


    });

    //初始化省信息
    function init_province() {
        $.ajax({
            url: '<%=path %>/admin/addressInfo/init_address.do',
            async: false,
            success: function (data) {
                $.each(data.data, function (idx, obj) {
                    var value = obj.id;
                    $('#province-select').append('<option value=' + value + '>' + obj.name + '</option>')
                });
            }
        });
    }

    //初始化市信息
    function init_city() {
        $.ajax({
            url: '<%=path %>/admin/addressInfo/init_address.do?pid=' + $('#province-select').val(),
            async: false,
            success: function (data) {
                $.each(data.data, function (idx, obj) {
                    var value = obj.id;
                    $('#city-select').append('<option value=' + value + '>' + obj.name + '</option>')
                });
            }
        });
    }

    //初始化业务A类型
    function init_one_bus_type() {
        $.ajax({
            url: '<%=path %>/admin/btype/init_one_type.do',
            async: false,
            success: function (data) {
                $.each(data.data, function (idx, obj) {
                    var value = obj.bId;
                    $('#bus-one-select').append('<option value=' + value + '>' + obj.bName + '</option>')
                });
            }
        });
    }

    //初始化业务B类型
    function init_two_bus_type() {
        $.ajax({
            url: '<%=path %>/admin/btype/get_two_type.do',
            async: false,
            success: function (data) {
                $.each(data.data, function (idx, obj) {
                    var value = obj.bId;
                    $('#bus-two-select').append('<option value=' + value + '>' + obj.bName + '</option>')
                });
            }
        });
    }

    /*初始化表格数据*/
    function initData() {
        $('#table').bootstrapTable({
            url: "<%=request.getContextPath() %>/admin/order/queryOrders.do",
            //clickToSelect: 'true',//选中一行
            queryParams: queryParams,
            formatLoadingMessage: function () {
                return "请稍等，正在加载中...";
            },
            formatNoMatches: function () {  //没有匹配的结果
                return '无符合条件的记录';
            },
            columns: [{
                field: 'SerialNumber',
                title: '序号',
                width: '80',
                align: 'center',
                formatter: function (value, row, index) {
                    return index+1;
                }
            }, {
                field: 'orderNo',
                title: '订单号',
                width: '200',
                align: 'center',
            }, {
                field: 'uname',
                title: '用户',
                width: '150',
                align: 'center',
            }, {
                field: 'bName',
                title: '业务详情',
                width: '250',
                align: 'center',
            }, {
                field: 'price',
                title: '价格',
                width: '100',
                align: 'center',
            }, {
                field: 'state',
                title: '订单状态',
                width: '150',
                align: 'center',
            }, {
                field: 'crtTime',
                title: '下单时间',
                width: '250',
                align: 'center',
            }, {
                field: 'operate',
                title: '操作',
                width: '350',
                align: 'center',
                formatter: 'operateFormatter',
            }],
        });
    }

    //选择省
    $('#province-select').on("change", function () {
        if ($('#province-select').val() == -1) {
            $('#province-select').find('option').remove();
            $('#city-select').find('option').remove();
            $('#area-select').find('option').remove();
            $('#province-select').append("<option value='-1'>全部</option>");
            $('#city-select').append("<option value='-1'>全部</option>");
            $('#area-select').append("<option value='-1'>全部</option>");
            init_province();
        } else {
            $.ajax({
                url: '<%=basePath %>/admin/addressInfo/init_address.do?pid=' + $('#province-select').val(),
                async: false,
                success: function (data) {
                    $('#city-select').find('option').remove();
                    $('#area-select').find('option').remove();
                    $('#area-select').append("<option value='-1'>全部</option>");
                    $('#city-select').append("<option value='-1'>全部</option>");
                    $.each(data.data, function (idx, obj) {
                        var value = obj.id;
                        $('#city-select').append('<option value=' + value + '>' + obj.name + '</option>')
                    });
                }
            });
        }
    });

    //选择市
    $('#city-select').on("change", function () {
        if ($('#city-select').val() == -1) {
            $('#city-select').find('option').remove();
            $('#area-select').find('option').remove();
            $('#city-select').append("<option value='-1'>全部</option>");
            $('#area-select').append("<option value='-1'>全部</option>");
            init_city();
        } else {
            $.ajax({
                url: '<%=basePath %>/admin/addressInfo/init_address.do?pid=' + $('#city-select').val(),
                async: false,
                success: function (data) {
                    //$('#city-select').find('option').remove();
                    $('#area-select').find('option').remove();
                    $('#area-select').append("<option value='-1'>全部</option>");
                    //$('#city-select').append("<option value='-1'>全部</option>");
                    $.each(data.data, function (idx, obj) {
                        var value = obj.id;
                        $('#area-select').append('<option value=' + value + '>' + obj.name + '</option>')
                    });
                }
            });
        }
    });

    //业务类型A
    $('#bus-one-select').on("change", function () {
        if ($('#bus-one-select').val() == -1) {
            $('#bus-one-select').find('option').remove();
            $('#bus-two-select').find('option').remove();
            $('#bus-three-select').find('option').remove();
            $('#bus-one-select').append("<option value='-1'>全部</option>");
            $('#bus-two-select').append("<option value='-1'>全部</option>");
            $('#bus-three-select').append("<option value='-1'>全部</option>");
            init_one_bus_type();
        } else {
            $.ajax({
                url: '<%=basePath %>/admin/btype/get_two_type.do?pid=' + $('#bus-one-select').val(),
                async: false,
                success: function (data) {
                    $('#bus-three-select').find('option').remove();
                    $('#bus-two-select').find('option').remove();
                    $('#bus-two-select').append("<option value='-1'>全部</option>");
                    $('#bus-three-select').append("<option value='-1'>全部</option>");
                    $.each(data.data, function (idx, obj) {
                        var value = obj.bId;
                        $('#bus-two-select').append('<option value=' + value + '>' + obj.bName + '</option>')
                    });
                }
            });
        }
    });

    //选择业务类型B
    $('#bus-two-select').on("change", function () {
        if ($('#bus-two-select').val() == -1) {
            $('#bus-three-select').find('option').remove();
            $('#bus-three-select').append("<option value='-1'>全部</option>");
            //init_city();
        } else {
            $.ajax({
                url: '<%=basePath %>/admin/bus/get_three_type.do?pid=' + $('#bus-two-select').val(),
                async: false,
                success: function (data) {
                    $('#bus-three-select').find('option').remove();
                    $('#bus-three-select').append("<option value='-1'>全部</option>");
                    $.each(data.data, function (idx, obj) {
                        var value = obj.id;
                        $('#bus-three-select').append('<option value=' + value + '>' + obj.bName + '</option>')
                    });
                }
            });
        }
    });

    /*刷新*/
    function search() {
        offset = 0;
        $table.bootstrapTable('refresh');
    }

    /**封装查询条件**/
    function queryParams(params) {
        if (offset == 0) {
            params.offset = offset;
        }
        offset = -1;
        var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit,  //页面大小
            offset: params.offset, //页码
            orderNo:$("#orderNo").val(),
            phone: $("#phone").val(),
            busaId: $("#bus-one-select").val(),
            busbId: $("#bus-two-select").val(),
            buscId: $("#bus-three-select").val(),
            provId: $("#province-select").val(),
            cityId: $("#city-select").val(),
            areaId: $("#area-select").val(),
            state: $("#state-select").val(),
            startTime: $("#startTime").val(),
            endTime: $("#endTime").val(),
            ide: $("#ide").val(),
            maxrows: params.limit,
            pageindex: params.pageNumber
        };
        return temp;
    }

    //操作
    function operateFormatter(value) {
        var stateV = value.stateValue;
        var orderNo = value.orderNo;
        var orderId = value.orderId;
        var content = '<a href="<%=basePath %>/admin/order/qOrderDetail.do?orderNo=' + orderNo + '" id="orderDetail" class="btn">订单详情</a>';
        content = content + '<input type="hidden" id="order' + orderId + '" value="' + orderNo + '" />';
        if (stateV == 2) {
            //content = content + '<a href="<=basePath %>/admin/worker/query_workers.do?orderNo=' + orderNo + '" id="disOrder" data-toggle="modal" class="btn">派单</a>';
            content = content + '<a href="javascript:queryWorkers(' + orderId + ')" id="disOrder" class="btn">派单</a>';
        } else if (stateV == 7) {
            content = content + '<a href="javascript:confirmRefund(' + orderId + ')" id="refund" class="btn">确认退款</a>';
        } else if (stateV == 4) {
            /*content = content + '<a href="<=basePath %>/admin/worker/worker_info.do?workerId=' + orderNo + '" id="finish" class="btn">完成订单</a>';*/
            content = content + '<a href="javascript:confirmOrderFinished(' + orderId + ')" id="finish" class="btn">完成订单</a>';
        }
           /* + 'c:if test="{stateV == 2}">'
            +    '<a href="<=basePath %>/admin/worker/worker_info.do?workerId={orderNo}" id="disOrder" class="btn">派单</a>'
            + '/c:if> '
            + 'c:if test="{stateV == 7}">'
            +    '<a href="<=basePath %>/admin/worker/worker_info.do?workerId={orderNo}" id="refund" class="btn">确认退款</a>'
            + '/c:if> '
            + 'c:if test="{stateV == 3 or stateV == 5}">'
            +    '<a href="<=basePath %>/admin/worker/worker_info.do?workerId={orderNo}" id="finish" class="btn">完成订单</a>'
            + '/c:if> ';*/
            return content;
    }

    function queryWorkers(id) {
        var orderNo = $("#order"+id).val();
        $.ajax({
            url: '<%=basePath %>admin/order/query_workers.do?orderNo=' + orderNo,
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data.code == '200') {
                    $('#worker-select').find('option').remove();
                    $('#worker-select').append('<option value=' + '-1' + '>' + '-请选择-' + '</option>');
                    /*$('#orderNo2').value = data.data.orderNo2;*/
                    $('#orderNo2').val(data.data.orderNo2);
                    $.each(data.data.workers, function (idx, obj) {
                        var value = obj.id;
                        $('#worker-select').append('<option value=' + value + '>' + obj.wName + '</option>')
                        $('#myModal').modal("show");
                    });
                } else {
                    warning_prompt("没有合适的师傅", 1500);
                }

            }
        });
    }

$("#dispatch").on("click", function () {
    var workerId = $("#worker-select").val();
    var orderNo = $('#orderNo2').val();
    $.ajax({
        type: "POST",
        url: "<%= basePath %>/admin/order/dispatchOrder.do",
        dataType:'json',
        data: {
            workerId : workerId,
            orderNo : orderNo
        },
        success: function(data){
            if(data.code=='200'){
                success_prompt("派单成功", 1500);
                window.location.reload();//刷新当前页面.
            }else{
                fail_prompt("派单失败", 1500);
            }
        }
    });
});

    //确认服务完成
    function confirmOrderFinished(id) {
        var orderNo = $("#order"+id).val();
        Ewin.confirm({ message: "确认服务已完成？" }).on(function (e) {
            if (!e) {
                return;
            }
            $.ajax({
                type: "POST",
                url: "<%= basePath %>/admin/order/confirmOrderFinished.do",
                dataType:'json',
                data: {
                    orderNo : orderNo
                },
                success: function(data){
                    if(data.code=='200'){
                        success_prompt("确认成功", 1500);
                        window.location.reload();//刷新当前页面.
                    }else{
                        fail_prompt("确认失败", 1500);
                    }
                }
            });
        });
    }
    
    function confirmRefund(id) {
        var orderNo = $("#order"+id).val();
        Ewin.confirm({ message: "确认退款？" }).on(function (e) {
            if (!e) {
                return;
            }
            //<=basePath %>/admin/worker/worker_info.do?workerId='+ orderNo
            $.ajax({
                type: "POST",
                url: "<%= basePath %>/admin/refund.do",
                dataType:'json',
                data: {
                    orderNo : orderNo
                },
                success: function(data){
                    if(data.code=='200'){
                        success_prompt("申请退款成功", 1500);
                        window.location.reload();//刷新当前页面.
                    }else{
                        fail_prompt(data.hint, 1500);
                    }
                }
            });
        });
    }

    jQuery(document).ready(function($)
    {
        selectEffect("admin/order/order_list_page.do");
    });
</script>
</body>
</html>

